package com.waitingsnow.common.share.netEaseMicroblog.search
{
	import com.waitingsnow.common.share.bridia.net.oauth.BDOAuthConnection;
	import com.waitingsnow.common.share.netEaseMicroblog.NEMicroblog;
	import com.waitingsnow.common.share.netEaseMicroblog.data.NEMObject;
	
	/**
	 * 
	 * @author Tensharp
	 * 
	 * <p>包含了所有的搜索接口.</p>
	 */	
	
	public class NEMSearch extends NEMObject
	{
		private var total:int;
		
		/**
		 * 
		 * <p>创建一个新的NEMSearch对象</p>
		 *  
		 * @param parent 上级对象.(NEMicroblog)
		 * @param oauth OAuth链接
		 * 
		 */		
		
		public function NEMSearch(parent:NEMicroblog, oauth:BDOAuthConnection)
		{
			super(parent, oauth);
		}
		
		///
		///	[Search]
		///
		
		/**
		 * 
		 * <p>search</p>
		 * <p>搜索微博。</p> 
		 * <p>未带q参数时会返回400状态</p>
		 * 
		 * @param q 必选参数，关键字,最大长度25,如果以#起始的关键字会作为tag搜索精确匹配
		 * @param page 可选参数，当前页数，默认为第一页
		 * @param per_page 可选参数，返回数量,最大20
		 * 
		 */		
		
		public function search (q:String, page:String = "1", per_page:String = "20", total:int = 0):void
		{
			this.total = total;
			
			//
			
			oauth.addOAuthParameters ("q", q, true);
			oauth.addOAuthParameters ("page", page, true);
			oauth.addOAuthParameters ("per_page", per_page, true);
			
			if (total > 0)
			{
				oauth.addOAuthParameters ("total", total.toString (), true);
			}

			//
			
			connect ("SEARCH");
		}
		
		///
		///	[Statuses search]
		///
		
		/**
		 * 
		 * <p>statuses search</p>
		 * <p>搜索微博。</p>
		 * <p>未带q参数时会返回400状态</p>
		 * 
		 * @param q 		必选参数 : 关键字,最大长度25,如果以#起始的关键字会作为tag搜索精确匹配
		 * @param page 		可选参数 : 当前页数，默认为第一页
		 * @param per_page 	可选参数 : 返回数量,最大20
		 * @param trim_user 必选参数 : 可选参数，值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量
		 * @param total 	可选参数 : 需要返回的结果总数.值为0时不传递.
		 * 
		 */	
		
		public function statuses_search (q:String, page:String = "1", per_page:String = "20", trim_user:Boolean = false, total:int = 0):void
		{
			this.total = total;
			
			//
			
			oauth.addOAuthParameters ("q", q, true);
			oauth.addOAuthParameters ("page", page, true);
			oauth.addOAuthParameters ("per_page", per_page, true);
			oauth.addOAuthParameters ("trim_user", trim_user.toString (), true);
			
			//
			
			if (total > 0)
			{
				oauth.addOAuthParameters ("total", total.toString (), true);
			}
			
			//
			
			connect ("USER_SEARCH");
		}
		
		///
		///	[users search]
		///
		
		/**
		 * 
		 * <p>users/search</p>
		 * <p>搜索用户。</p>
		 *  
		 * @param q 必选参数，关键字,最大长度25
		 * @param page 		可选参数，当前页数，默认为第一页
		 * @param per_page 	可选参数，返回数量,最大20
		 * @param total 	可选参数 : 需要返回的结果总数.值为0时不传递.
		 * 
		 */	
		
		public function user_search (q:String, page:String = "1", per_page:String = "20", total:int = 0):void
		{
			this.total = total;
			
			//
			
			oauth.addOAuthParameters ("q", q, true);
			oauth.addOAuthParameters ("page", page, true);
			oauth.addOAuthParameters ("per_page", per_page, true);
			
			if (total > 0)
			{
				oauth.addOAuthParameters ("total", total.toString (), true);
			}
			
			//
			
			connect ("USER_SEARCH");
		}
		
		/**
		 * @private
		 * 
		 * <p>OAuth链接</p>
		 *  
		 * @param action
		 * @param method
		 * @param hasFormat
		 * @param replace
		 * 
		 */		
		
		override protected function connect(action:String, method:String="GET", hasFormat:Boolean=true, replace:String=null):void
		{
			super.action = action;
			
			//
			
			var url:String;
			
			if (total > 0)
			{
				url = uri ("/" + total + action, hasFormat);
			}
			else
			{
				url = uri (action, hasFormat);
			}
			
			//
			
			loader.load (oauth.getOAuthRequest (method, url));
		}
	}
}